<?php
/**
 * 分页类
 * Created by PhpStorm.
 * User: YueYa
 * Date: 2016-12-13
 * Time: 16:56
 */

namespace common\components;

class PageView{

	public $dataCount;
	public $perPage;
	public $pageCount;
	public $currentPage;

	public $offset;
	public $limit;
	public $limitSql;

	public $prevPage;
	public $nextPage;

	public function __construct($dataCount, $perPage = 10, $currentPage = 1){
		$this->dataCount = $dataCount;
		$this->perPage = $perPage;
		$this->currentPage = $currentPage;
		$this->init();
	}

	public function init(){
		//计算总页数 $pagesum
		if($this->dataCount <= 0){
			$this->pageCount = 1;
		}else if($this->dataCount % $this->perPage == 0){
			$this->pageCount = intval($this->dataCount / $this->perPage);
		}else{
			$this->pageCount = intval($this->dataCount / $this->perPage) + 1;
		}

		//修正当前页
		if($this->currentPage < 1)$this->currentPage = 1;
		if($this->currentPage > $this->pageCount){
			$this->currentPage = $this->pageCount;
		}

		//计算当前页起始数据
		$this->offset = ($this->currentPage -1) * $this->perPage;

		$remainder = $this->dataCount % $this->perPage;//余数

		$this->limit = ($this->currentPage == $this->pageCount && $remainder != 0) ? $remainder : $this->perPage;
		$this->limitSql = ($this->offset == 0) ? ' LIMIT '.$this->limit : ' LIMIT '.$this->offset.", ".$this->limit;

		//计算上一页和下一页
		$this->prevPage = $this->currentPage > 1 ? $this->currentPage - 1 : 1;
		$this->nextPage = $this->currentPage < $this->pageCount ? $this->currentPage + 1 : $this->pageCount;

	}

	/**
	 * 分屏
	 */
	static function byscreen($datasum, $evepage, $curpage, $evescreen, $curscreen){
		$page = self::bypage($datasum, $evepage, $curpage);
//		echo "<pre>"; print_r($page); echo "</pre>";
		$screen = self::bypage($page["data"]["curPageData"], $evescreen, $curscreen);
		return array(
			'page' => $page['page'],
			'screen' => array('sum' => $screen['page']['sum'], 'currentScreen'=> $screen['page']['curPage']),
			'data' => array(
				'sum' => $datasum, 
				'everyPageData' => $evepage, 
				'startPageData' => $page["data"]["startData"], 
				'currentPageData' =>$page["data"]["curPageData"],
				'everyScreenData' => $evescreen, 
				'startScreenData' => ($page["data"]["startData"] + $screen["data"]["startData"]), 
				'currentScreenData' =>$screen["data"]["curPageData"]
			)
		);
	}

}

?>